<template>
  <el-dialog width="370px"
             :visible.sync="visible"
             @closed="handleClosed"
             append-to-body
             destroy-on-close>
    <div class="del-app-dialog flex flex-col items-center">
      <img class="del-app-dialog--img"
           src="@/assets/img/notification.png"
           alt=""
           width="74">

      <strong class="del-app-dialog--text">Are you sure you want to delete {{params.name}}?</strong>

      <div class="del-app-dialog--tip">Once an app is deleted,it cannot be recovered.</div>

      <el-checkbox v-model="form.checkbox">Delete the app of the tenant.</el-checkbox>

      <el-button @click="handleCancel"
                 class="el-button--full"
                 size="mini">{{$t('cancel')}}</el-button>
      <el-button type="danger"
                 class="el-button--full el-button--smt ml-0"
                 size="mini"
                 :loading="loading"
                 @click="handleDelete">{{$t('delete')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'DelAppDialog',
  props: {
    params: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      visible: false,
      form: {
        checkbox: false
      },
      loading: false
    }
  },

  methods: {
    handleCancel () {
      this.visible = false
    },
    handleClosed () {
      this.form.checkbox = false
      this.$emit('closed')
    },
    async handleDelete () {
      const { get } = this._
      try {
        this.loading = true

        const { deletePrebuilt } = this.$interface.reports

        const params = {
          id: this.params.appId,
          ...this.form
        }

        let { data: response } = await deletePrebuilt(params)

        if (response.code !== 0) {
          throw get(response, 'msg', '')
        }

        this.$message.success('success')
        this.handleCancel()
        this.$emit('successed')
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.del-app-dialog {
  &--text {
    font-size: 18px;
    color: #404040;
    line-height: 26px;
    text-align: center;
    padding: 38px 0 15px;
  }
  &--tip {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.5);
    line-height: 20px;
  }
  .el-checkbox {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.5);
    margin: 15px 0 38px;
  }
}
</style>
